package com.ruoyi.web.controller.module.product;

import java.util.*;

import cn.hutool.core.collection.CollUtil;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.system.domain.module.product.ProductRule;
import com.ruoyi.system.domain.module.product.ProductRuleRelation;
import com.ruoyi.system.domain.module.vo.ProductRuleVO;
import com.ruoyi.system.service.module.product.IProductRuleService;
import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.module.product.IProductRuleRelationService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 规格和商家服务类型Controller
 *
 * @author ruoyi
 * @date 2022-02-06
 */
@Validated
@Api(value = "规格和商家服务类型控制器", tags = {"规格和商家服务类型管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/module/rule/relation")
public class ProductRuleRelationController extends BaseController {

    private final IProductRuleRelationService productRuleRelationService;
	private final IProductRuleService productRuleService;

    /**
     * 查询规格和商家服务类型列表
     */
    @ApiOperation("查询规格和商家服务类型列表")
//    @PreAuthorize("@ss.hasPermi('system:relation:list')")
    @GetMapping("/list")
    public TableDataInfo<ProductRuleRelation> list(@Validated ProductRuleRelation productRuleRelation) {
        QueryWrapper<ProductRuleRelation> qw = new QueryWrapper<>();
        if(productRuleRelation.getCateChildId() != null){
            qw.eq("cate_child_id",productRuleRelation.getCateChildId());
        }
        TableDataInfo<ProductRuleRelation> td = PageUtils.buildDataInfo(productRuleRelationService.page(PageUtils.buildPage(),qw));
        List<ProductRuleRelation> list = td.getRows();
        return td;
    }


	/**
	 * 查询规格和商家服务类型列表
	 */
	@ApiOperation("查询规格和商家服务类型列表")
	@PreAuthorize("@ss.hasPermi('module:ruleRelation:list')")
	@GetMapping("/getTransferData")
	public AjaxResult<Map<String,Object>> getTransferData(@Validated ProductRuleRelation productRuleRelation) {
		QueryWrapper<ProductRuleRelation> qw = new QueryWrapper<>();
		if(productRuleRelation.getCateId() != null){
			qw.eq("cate_id",productRuleRelation.getCateId());
		}
		if(productRuleRelation.getCateChildId() != null){
			qw.eq("cate_child_id",productRuleRelation.getCateChildId());
		}

		Map<String, Object> map = new HashMap<>();
		List<Integer> valueRuleIds = new ArrayList<>();
		List<ProductRuleVO> valueRuleList = new ArrayList<>();
		List<ProductRuleRelation> list = productRuleRelationService.list(qw);
		if(CollUtil.isNotEmpty(list)){
			for (ProductRuleRelation relation : list){
				ProductRule rule = productRuleService.findById(relation.getRuleId());
				if(rule != null){
					ProductRuleVO ruleVO = new ProductRuleVO();
					ruleVO.setRuleId(rule.getId());
					ruleVO.setRuleName(rule.getRuleName());
					valueRuleIds.add(rule.getId());
					valueRuleList.add(ruleVO);
				}
			}
		}
		map.put("valueRuleList", valueRuleList);
		map.put("valueRuleIds", valueRuleIds);

		List<ProductRuleVO> allRuleList = new ArrayList<>();
		List<ProductRule> allDataList = productRuleService.list();
		if(CollUtil.isNotEmpty(allDataList)){
			for (ProductRule rule : allDataList){
				ProductRuleVO ruleVO = new ProductRuleVO();
				ruleVO.setRuleId(rule.getId());
				ruleVO.setRuleName(rule.getRuleName());
				allRuleList.add(ruleVO);
			}
		}
		map.put("allRuleList", allRuleList);
		return AjaxResult.success(map);
	}

    /**
     * 导出规格和商家服务类型列表
     */
    @ApiOperation("导出规格和商家服务类型列表")
//    @PreAuthorize("@ss.hasPermi('system:relation:export')")
    @Log(title = "规格和商家服务类型", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<ProductRuleRelation> export(@Validated ProductRuleRelation productRuleRelation) {
        List<ProductRuleRelation> list = productRuleRelationService.list();
        ExcelUtil<ProductRuleRelation> util = new ExcelUtil<ProductRuleRelation>(ProductRuleRelation.class);
        return util.exportExcel(list, "规格和商家服务类型");
    }

    /**
     * 获取规格和商家服务类型详细信息
     */
    @ApiOperation("获取规格和商家服务类型详细信息")
//    @PreAuthorize("@ss.hasPermi('system:relation:query')")
    @GetMapping("/{id}")
    public AjaxResult<ProductRuleRelation> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        ProductRuleRelation productRuleRelation = productRuleRelationService.getById(id);
        return AjaxResult.success(productRuleRelation);
    }

    /**
     * 新增规格和商家服务类型
     */
    @ApiOperation("新增规格和商家服务类型")
//    @PreAuthorize("@ss.hasPermi('system:relation:add')")
    @Log(title = "规格和商家服务类型", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody ProductRuleRelation productRuleRelation) {
        return toAjax(productRuleRelationService.save(productRuleRelation) ? 1 : 0);
    }

    /**
     * 修改规格和商家服务类型
     */
    @ApiOperation("修改规格和商家服务类型")
//    @PreAuthorize("@ss.hasPermi('system:relation:edit')")
    @Log(title = "规格和商家服务类型", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody ProductRuleRelation productRuleRelation) {
        return toAjax(productRuleRelationService.updateById(productRuleRelation) ? 1 : 0);
    }

    /**
     * 删除规格和商家服务类型
     */
    @ApiOperation("删除规格和商家服务类型")
//    @PreAuthorize("@ss.hasPermi('system:relation:remove')")
    @Log(title = "规格和商家服务类型" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return toAjax(productRuleRelationService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }



	/**
	 * 删除规格和商家服务类型
	 */
	@ApiOperation("删除规格和商家服务类型")
	@PreAuthorize("@ss.hasPermi('module:ruleRelation:remove')")
	@Log(title = "规格和商家服务类型" , businessType = BusinessType.DELETE)
	@DeleteMapping("/removeByRuleId")
	public AjaxResult<Void> removeByRuleId(@RequestBody ProductRuleRelation productRuleRelation) {

		QueryWrapper<ProductRuleRelation> qw = new QueryWrapper<>();
		qw.eq("rule_id", productRuleRelation.getRuleId());
		qw.eq("cate_id", productRuleRelation.getCateId());
		if(productRuleRelation.getCateChildId()!= null){
			qw.eq("cate_child_id", productRuleRelation.getCateChildId());
		}
		qw.last("limit 1");
		ProductRuleRelation relation = productRuleRelationService.getOne(qw);
		if(relation != null){
			boolean bl = productRuleRelationService.removeById(relation.getId());
			return toAjax(bl ? 1 : 0);
		}
		return toAjax(0);
	}
}
